package com.ujson.uaa.service.impl;

import com.ujson.base.common.exception.GlobalException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * <h3>ujson-parent</h3>
 * <p>自定义密码编码器</p>
 *
 * @author JerryLiu  a1031757312@gmail.com
 * @version 1.0
 * @date 2021-11-20 13:21
 */
@Component
public class CustomPasswordEncoder extends BCryptPasswordEncoder {
    @Override
    public String encode(CharSequence rawPassword) {
        return rawPassword.toString();
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        // 密码先进行简单验证
        if (StringUtils.isBlank(rawPassword)) {
            throw new GlobalException("raw password cannot be empty!");
        }
        if (StringUtils.isBlank(encodedPassword)) {
            throw new GlobalException("please input password!");
        }
        return rawPassword.equals(encodedPassword);
    }
}
